package com.demo.intercept;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

import java.util.Map;

/**
 * @Version 2019
 * @Author:kele
 * @Date:2020/4/20
 * @Content:
 */
public class MyIntercept extends MethodFilterInterceptor {
    @Override
    protected String doIntercept(ActionInvocation invocation) throws Exception {

        System.out.println("拦截器执行了。。。");

        //获得session
        Map<String, Object> session = ActionContext.getContext().getSession();
        //获得session的数据
        Object object = session.get("user");
        //判断session是否有user
        if (object != null) {
            //放行
            return invocation.invoke();
        } else {
            return "toLogin";
        }

    }
}
